namespace Dotnet.Utils.Utility.BarCode
{
    public class DataMatrixReed
    {
        private int[] gfi = new int[] { 
            1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0x2d, 90, 180, 0x45, 0x8a, 0x39, 0x72, 0xe4, 
            0xe5, 0xe7, 0xe3, 0xeb, 0xfb, 0xdb, 0x9b, 0x1b, 0x36, 0x6c, 0xd8, 0x9d, 0x17, 0x2e, 0x5c, 0xb8, 
            0x5d, 0xba, 0x59, 0xb2, 0x49, 0x92, 9, 0x12, 0x24, 0x48, 0x90, 13, 0x1a, 0x34, 0x68, 0xd0, 
            0x8d, 0x37, 110, 220, 0x95, 7, 14, 0x1c, 0x38, 0x70, 0xe0, 0xed, 0xf7, 0xc3, 0xab, 0x7b, 
            0xf6, 0xc1, 0xaf, 0x73, 230, 0xe1, 0xef, 0xf3, 0xcb, 0xbb, 0x5b, 0xb6, 0x41, 130, 0x29, 0x52, 
            0xa4, 0x65, 0xca, 0xb9, 0x5f, 190, 0x51, 0xa2, 0x69, 210, 0x89, 0x3f, 0x7e, 0xfc, 0xd5, 0x87, 
            0x23, 70, 140, 0x35, 0x6a, 0xd4, 0x85, 0x27, 0x4e, 0x9c, 0x15, 0x2a, 0x54, 0xa8, 0x7d, 250, 
            0xd9, 0x9f, 0x13, 0x26, 0x4c, 0x98, 0x1d, 0x3a, 0x74, 0xe8, 0xfd, 0xd7, 0x83, 0x2b, 0x56, 0xac, 
            0x75, 0xea, 0xf9, 0xdf, 0x93, 11, 0x16, 0x2c, 0x58, 0xb0, 0x4d, 0x9a, 0x19, 50, 100, 200, 
            0xbd, 0x57, 0xae, 0x71, 0xe2, 0xe9, 0xff, 0xd3, 0x8b, 0x3b, 0x76, 0xec, 0xf5, 0xc7, 0xa3, 0x6b, 
            0xd6, 0x81, 0x2f, 0x5e, 0xbc, 0x55, 170, 0x79, 0xf2, 0xc9, 0xbf, 0x53, 0xa6, 0x61, 0xc2, 0xa9, 
            0x7f, 0xfe, 0xd1, 0x8f, 0x33, 0x66, 0xcc, 0xb5, 0x47, 0x8e, 0x31, 0x62, 0xc4, 0xa5, 0x67, 0xce, 
            0xb1, 0x4f, 0x9e, 0x11, 0x22, 0x44, 0x88, 0x3d, 0x7a, 0xf4, 0xc5, 0xa7, 0x63, 0xc6, 0xa1, 0x6f, 
            0xde, 0x91, 15, 30, 60, 120, 240, 0xcd, 0xb7, 0x43, 0x86, 0x21, 0x42, 0x84, 0x25, 0x4a, 
            0x94, 5, 10, 20, 40, 80, 160, 0x6d, 0xda, 0x99, 0x1f, 0x3e, 0x7c, 0xf8, 0xdd, 0x97, 
            3, 6, 12, 0x18, 0x30, 0x60, 0xc0, 0xad, 0x77, 0xee, 0xf1, 0xcf, 0xb3, 0x4b, 150
         };
        private int[] gflog = new int[] { 
            0, 0, 1, 240, 2, 0xe1, 0xf1, 0x35, 3, 0x26, 0xe2, 0x85, 0xf2, 0x2b, 0x36, 210, 
            4, 0xc3, 0x27, 0x72, 0xe3, 0x6a, 0x86, 0x1c, 0xf3, 140, 0x2c, 0x17, 0x37, 0x76, 0xd3, 0xea, 
            5, 0xdb, 0xc4, 0x60, 40, 0xde, 0x73, 0x67, 0xe4, 0x4e, 0x6b, 0x7d, 0x87, 8, 0x1d, 0xa2, 
            0xf4, 0xba, 0x8d, 180, 0x2d, 0x63, 0x18, 0x31, 0x38, 13, 0x77, 0x99, 0xd4, 0xc7, 0xeb, 0x5b, 
            6, 0x4c, 220, 0xd9, 0xc5, 11, 0x61, 0xb8, 0x29, 0x24, 0xdf, 0xfd, 0x74, 0x8a, 0x68, 0xc1, 
            0xe5, 0x56, 0x4f, 0xab, 0x6c, 0xa5, 0x7e, 0x91, 0x88, 0x22, 9, 0x4a, 30, 0x20, 0xa3, 0x54, 
            0xf5, 0xad, 0xbb, 0xcc, 0x8e, 0x51, 0xb5, 190, 0x2e, 0x58, 100, 0x9f, 0x19, 0xe7, 50, 0xcf, 
            0x39, 0x93, 14, 0x43, 120, 0x80, 0x9a, 0xf8, 0xd5, 0xa7, 200, 0x3f, 0xec, 110, 0x5c, 0xb0, 
            7, 0xa1, 0x4d, 0x7c, 0xdd, 0x66, 0xda, 0x5f, 0xc6, 90, 12, 0x98, 0x62, 0x30, 0xb9, 0xb3, 
            0x2a, 0xd1, 0x25, 0x84, 0xe0, 0x34, 0xfe, 0xef, 0x75, 0xe9, 0x8b, 0x16, 0x69, 0x1b, 0xc2, 0x71, 
            230, 0xce, 0x57, 0x9e, 80, 0xbd, 0xac, 0xcb, 0x6d, 0xaf, 0xa6, 0x3e, 0x7f, 0xf7, 0x92, 0x42, 
            0x89, 0xc0, 0x23, 0xfc, 10, 0xb7, 0x4b, 0xd8, 0x1f, 0x53, 0x21, 0x49, 0xa4, 0x90, 0x55, 170, 
            0xf6, 0x41, 0xae, 0x3d, 0xbc, 0xca, 0xcd, 0x9d, 0x8f, 0xa9, 0x52, 0x48, 0xb6, 0xd7, 0xbf, 0xfb, 
            0x2f, 0xb2, 0x59, 0x97, 0x65, 0x5e, 160, 0x7b, 0x1a, 0x70, 0xe8, 0x15, 0x33, 0xee, 0xd0, 0x83, 
            0x3a, 0x45, 0x94, 0x12, 15, 0x10, 0x44, 0x11, 0x79, 0x95, 0x81, 0x13, 0x9b, 0x3b, 0xf9, 70, 
            0xd6, 250, 0xa8, 0x47, 0xc9, 0x9c, 0x40, 60, 0xed, 130, 0x6f, 20, 0x5d, 0x7a, 0xb1, 150
         };
        private int K;
        private int N = 0xff;
        private int[] p;
        private static int[] p10 = new int[] { 0x3d, 110, 0xff, 0x74, 0xf8, 0xdf, 0xa6, 0xb9, 0x18, 0x1c };
        private static int[] p11 = new int[] { 120, 0x61, 60, 0xf5, 0x27, 0xa8, 0xc2, 12, 0xcd, 0x8a, 0xaf };
        private static int[] p12 = new int[] { 0xf2, 100, 0xb2, 0x61, 0xd5, 0x8e, 0x2a, 0x3d, 0x5b, 0x9e, 0x99, 0x29 };
        private static int[] p14 = new int[] { 0xb9, 0x53, 0xba, 0x12, 0x2d, 0x8a, 0x77, 0x9d, 9, 0x5f, 0xfc, 0xc0, 0x61, 0x9c };
        private static int[] p18 = new int[] { 
            0xbc, 90, 0x30, 0xe1, 0xfe, 0x5e, 0x81, 0x6d, 0xd5, 0xf1, 0x3d, 0x42, 0x4b, 0xbc, 0x27, 100, 
            0xc3, 0x53
         };
        private static int[] p20 = new int[] { 
            0xac, 0xba, 0xae, 0x1b, 0x52, 0x6c, 0x4f, 0xfd, 0x91, 0x99, 160, 0xbc, 2, 0xa8, 0x47, 0xe9, 
            9, 0xf4, 0xc3, 15
         };
        private static int[] p24 = new int[] { 
            0xc1, 50, 0x60, 0xb8, 0xb5, 12, 0x7c, 0xfe, 0xac, 5, 0x15, 0x9b, 0xdf, 0xfb, 0xc5, 0x9b, 
            0x15, 0xb0, 0x27, 0x6d, 0xcd, 0x58, 190, 0x34
         };
        private static int[] p28 = new int[] { 
            0xff, 0x5d, 0xa8, 0xe9, 0x97, 120, 0x88, 0x8d, 0xd5, 110, 0x8a, 0x11, 0x79, 0xf9, 0x22, 0x4b, 
            0x35, 170, 0x97, 0x25, 0xae, 0x67, 0x60, 0x47, 0x61, 0x2b, 0xe7, 0xd3
         };
        private static int[] p36 = new int[] { 
            0x70, 0x51, 0x62, 0xe1, 0x19, 0x3b, 0xb8, 0xaf, 0x2c, 0x73, 0x77, 0x5f, 0x89, 0x65, 0x21, 0x44, 
            4, 2, 0x12, 0xe5, 0xb6, 80, 0xfb, 220, 0xb3, 0x54, 120, 0x66, 0xb5, 0xa2, 250, 130, 
            0xda, 0xf2, 0x7f, 0xf5
         };
        private static int[] p42 = new int[] { 
            5, 9, 5, 0xe2, 0xb1, 150, 50, 0x45, 0xca, 0xf8, 0x65, 0x36, 0x39, 0xfd, 1, 0x15, 
            0x79, 0x39, 0x6f, 0xd6, 0x69, 0xa7, 9, 100, 0x5f, 0xaf, 8, 0xf2, 0x85, 0xf5, 2, 0x7a, 
            0x69, 0xf7, 0x99, 0x16, 0x26, 0x13, 0x1f, 0x89, 0xc1, 0x4d
         };
        private static int[] p48 = new int[] { 
            0x13, 0xe1, 0xfd, 0x5c, 0xd5, 0x45, 0xaf, 160, 0x93, 0xbb, 0x57, 0xb0, 0x2c, 0x52, 240, 0xba, 
            0x8a, 0x42, 100, 120, 0x58, 0x83, 0xcd, 170, 90, 0x25, 0x17, 0x76, 0x93, 0x10, 0x6a, 0xbf, 
            0x57, 0xed, 0xbc, 0xcd, 0xe7, 0xee, 0x85, 0xee, 0x16, 0x75, 0x20, 0x60, 0xdf, 0xac, 0x84, 0xf5
         };
        private int[] p5 = new int[] { 0x3e, 0x6f, 15, 0x30, 0xe4 };
        private static int[] p56 = new int[] { 
            0x2e, 0x8f, 0x35, 0xe9, 0x6b, 0xcb, 0x2b, 0x9b, 0x1c, 0xf7, 0x43, 0x7f, 0xf5, 0x89, 13, 0xa4, 
            0xcf, 0x3e, 0x75, 0xc9, 150, 0x16, 0xee, 0x90, 0xe8, 0x1d, 0xcb, 0x75, 0xea, 0xda, 0x92, 0xe4, 
            0x36, 0x84, 200, 0x26, 0xdf, 0x24, 0x9f, 150, 0xeb, 0xd7, 0xc0, 230, 170, 0xaf, 0x1d, 100, 
            0xd0, 220, 0x11, 12, 0xee, 0xdf, 9, 0xaf
         };
        private static int[] p62 = new int[] { 
            0xcc, 11, 0x2f, 0x56, 0x7c, 0xe0, 0xa6, 0x5e, 7, 0xe8, 0x6b, 4, 170, 0xb0, 0x1f, 0xa3, 
            0x11, 0xbc, 130, 40, 10, 0x57, 0x3f, 0x33, 0xda, 0x1b, 6, 0x93, 0x2c, 0xa1, 0x47, 0x72, 
            0x40, 0xaf, 0xdd, 0xb9, 0x6a, 250, 190, 0xc5, 0x3f, 0xf5, 230, 0x86, 0x70, 0xb9, 0x25, 0xc4, 
            0x6c, 0x8f, 0xbd, 0xc9, 0xbc, 0xca, 0x76, 0x27, 210, 0x90, 50, 0xa9, 0x5d, 0xf2
         };
        private static int[] p68 = new int[] { 
            0xba, 0x52, 0x67, 0x60, 0x3f, 0x84, 0x99, 0x6c, 0x36, 0x40, 0xbd, 0xd3, 0xe8, 0x31, 0x19, 0xac, 
            0x34, 0x3b, 0xf1, 0xb5, 0xef, 0xdf, 0x88, 0xe7, 210, 0x60, 0xe8, 220, 0x19, 0xb3, 0xa7, 0xca, 
            0xb9, 0x99, 0x8b, 0x42, 0xec, 0xe3, 160, 15, 0xd5, 0x5d, 0x7a, 0x44, 0xb1, 0x9e, 0xc5, 0xea, 
            180, 0xf8, 0x88, 0xd5, 0x7f, 0x49, 0x24, 0x9a, 0xf4, 0x93, 0x21, 0x59, 0x38, 0x9f, 0x95, 0xfb, 
            0x59, 0xad, 0xe4, 220
         };
        private static int[] p7 = new int[] { 0xfe, 0x5c, 240, 0x86, 0x90, 0x44, 0x17 };

        private int gfadd(int i, int j)
        {
            return (i ^ j);
        }

        private int mult(byte i, byte j)
        {
            int index = 0;
            if ((i == 0) || (j == 0))
            {
                return 0;
            }
            index = this.gflog[i] + this.gflog[j];
            if (index >= this.N)
            {
                index -= this.N;
            }
            return this.gfi[index];
        }

        public void ReedSolomon(int[] nCode, int i, int j)
        {
            this.setpolynomial(j);
            for (int k = i; k < (i + j); k++)
            {
                nCode[k] = 0;
            }
            for (int m = 0; m < i; m++)
            {
                byte num3 = (byte) (((byte) nCode[i]) ^ ((byte) nCode[m]));
                for (int n = 0; n < j; n++)
                {
                    nCode[i + n] = (byte) (((byte) nCode[(i + n) + 1]) ^ ((byte) this.mult(num3, (byte) this.p[n])));
                }
                nCode[(i + j) - 1] = (byte) this.mult(num3, (byte) this.p[j - 1]);
            }
        }

        public void setpolynomial(int i)
        {
            switch (i)
            {
                case 0x18:
                    this.p = p24;
                    return;

                case 0x1c:
                    this.p = p28;
                    return;

                case 5:
                    this.p = this.p5;
                    return;

                case 7:
                    this.p = p7;
                    return;

                case 10:
                    this.p = p10;
                    return;

                case 11:
                    this.p = p11;
                    return;

                case 12:
                    this.p = p12;
                    return;

                case 14:
                    this.p = p14;
                    return;

                case 0x12:
                    this.p = p18;
                    return;

                case 20:
                    this.p = p20;
                    return;

                case 0x24:
                    this.p = p36;
                    return;

                case 0x2a:
                    this.p = p42;
                    return;

                case 0x30:
                    this.p = p48;
                    return;

                case 0x38:
                    this.p = p56;
                    return;

                case 0x3e:
                    this.p = p62;
                    return;
            }
            this.p = p68;
        }
    }
}

